import { getTopCategoryAPI } from '@/apis/category'
import { onMounted, ref } from 'vue'
import { onBeforeRouteUpdate, useRoute } from 'vue-router'
export function useCategory() {
    const topcategory = ref([])
    const route = useRoute()

    const getTopCategory = async (id) => {
        const { result } = await getTopCategoryAPI(id)
        topcategory.value = result
        console.log(topcategory.value);

    }
    onMounted(async () => {
        getTopCategory(route.params.id)

    })
    // 目标:路由参数变化的时候 可以把分类数据接口重新发送
    onBeforeRouteUpdate((to) => {
        // console.log(to);
        // 存在问题：使用最新的路由参数请求最新的分类数据
        getTopCategory(to.params.id)

    })
    return {
        topcategory
    }
}